<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <dept-tree :node-data="company" :is-root="true" @addDept="addDept" />
        <hr>
        <el-tree :data="depData" :props="{label: 'name'}" default-expand-all>
          <template #default="{data}">
            <dept-tree :node-data="data" @delDepartment="Departments" @addDept="addDept" @addEditor="addEditor" />
          </template>
        </el-tree>
      </el-card>
      <!-- 新增弹窗 -->
      <add-dept ref="addDept" :no-data="noData" :show-dialog.sync="showDialog" @showDialog="showDialog" />
    </div>
  </div>
</template>

<script>
import deptTree from './components/dept-tree'
import addDept from './components/add-dept.vue'
import { getDepartments } from '@/api/departhment'
import { listToTreeData } from '@/utils'
export default {
  components: { deptTree, addDept },
  data() {
    return {
      company: {
        name: '不差钱国际有限公司',
        manager: '负责人',
        id: ''
      },
      depData: [],
      defaultProps: {
        children: 'children',
        label: 'department'
      },
      /* 新增弹窗 */
      showDialog: false,
      noData: {},
      /* 编辑弹窗 */
      showEditor: false
    }
  },
  created() {
    this.Departments()
  },
  methods: {
    async Departments() {
      const { depts } = await getDepartments()
      console.log(depts, 37)
      this.depData = listToTreeData(depts, '')
    },
    // 新增
    addDept(noData) {
      this.noData = noData
      this.showDialog = true
    },
    // 编辑
    async addEditor(noData) {
      console.log(this)
      await this.$refs.addDept.getDepartmentDetail(noData.id)
      this.noData = noData
      this.showDialog = true
    }
  }
}
</script>

<style>

</style>
